/**
 * 会议室管理模块配置文件
 * 该文件定义了会议室管理模块的相关配置，包括模块名称、API 路径、会议室接口定义、表格列配置、检索配置和添加配置。
 * @module meetingRoomsConfig
 */

import type { MeetingRoom as ApiMeetingRoom } from '/@/types/api'

// 扩展 API 的 MeetingRoom 类型
export interface MeetingRoomExtended extends Omit<ApiMeetingRoom, 'equipment'> {
  equipment: string | string[]
}

export type RoomStatusKey = 'UNOPEN' | 'IDLE'
export type RoomStatusValue = '未开放' | '空闲'
export const ROOM_STATUS: Record<RoomStatusKey, RoomStatusValue> = {
  UNOPEN: '未开放',
  IDLE: '空闲',
} as const

export const searchFields = [
  {
    label: '会议室位置',
    prop: 'positionDescription',
    component: 'select',
    placeholder: '请选择会议室位置',
    filterable: true,
    componentProps: {
      remote: true,
      remoteMethod: undefined,
      loading: undefined,
      options: [],
      clearable: true,
      filterable: true,
    },
  },
  {
    label: '会议室名称',
    prop: 'roomName',
    component: 'input',
    placeholder: '请选择会议室名称',
  },
  {
    label: '状态',
    prop: 'openStatus',
    component: 'select',
    placeholder: '请选择状态',
    options: Object.values(ROOM_STATUS).map((value) => ({
      label: value,
      value,
    })),
  },
]

export const tableColumns = [
  {
    label: '会议室名称',
    prop: 'roomName',
    minWidth: 200,
    showOverflowTooltip: true,
    customRender: true,
  },
  {
    label: '设备配置',
    prop: 'equipmentDisplay',
    minWidth: 200,
    customRender: true,
  },
  {
    label: '会议室位置',
    prop: 'positionDescription',
    minWidth: 200,
    customRender: true,
  },
  {
    label: '状态',
    prop: 'openStatus',
    width: 120,
    customRender: true,
  },
  {
    label: '操作',
    prop: 'operations',
    width: 220,
    fixed: 'right',
    customRender: true,
  },
]

export const formFields = [
  {
    label: '会议室名称',
    prop: 'roomName',
    component: 'input',
    required: true,
    fullWidth: true,
    placeholder: '请输入会议室名称',
    rules: [{ required: true, message: '请输入会议室名称' }],
  },
  {
    label: '容纳人数（人）',
    prop: 'capacity',
    component: 'input-number',
    required: true,
    colSpan: 12,
    placeholder: '请输入容纳人数',
    componentProps: {
      min: 1,
      controlsPosition: 'right',
    },
    rules: [{ required: true, message: '请输入容纳人数' }],
  },
  {
    label: '面积（㎡）',
    prop: 'area',
    component: 'input-number',
    required: true,
    colSpan: 12,
    placeholder: '请输入会议室面积',
    componentProps: {
      min: 1,
      controlsPosition: 'right',
    },
    rules: [{ required: true, message: '请输入面积' }],
  },
  {
    label: '位置描述',
    prop: 'positionDescription',
    component: 'input',
    type: 'textarea',
    required: true,
    fullWidth: true,
    placeholder: '请输入会议室位置描述，如：3号楼4层东侧',
    componentProps: {
      rows: 2,
    },
    rules: [{ required: true, message: '请输入位置描述' }],
  },
  {
    label: '设备配置',
    prop: 'equipment',
    component: 'select',
    multiple: true,
    filterable: true,
    allowCreate: true,
    defaultFirstOption: true,
    clearable: true,
    fullWidth: true,
    placeholder: '请选择或输入会议室配备的设备（可多选）',
    componentProps: {
      collapseTags: false,
      collapseTagsTooltip: false,
      popperClass: 'equipment-select-dropdown',
      style: 'min-height: 40px;',
    },
    options: [
      { label: '投影仪', value: '投影仪' },
      { label: '网络', value: '网络' },
      { label: '电脑', value: '电脑' },
      { label: '白板', value: '白板' },
      { label: '音响', value: '音响' },
      { label: '视频会议', value: '视频会议' },
    ],
  },
  {
    label: '联系人',
    prop: 'linkman',
    component: 'input',
    required: true,
    colSpan: 12,
    placeholder: '请输入联系人姓名',
    rules: [{ required: true, message: '请输入联系人' }],
  },
  {
    label: '联系电话',
    prop: 'linkmanPhone',
    component: 'input',
    colSpan: 12,
    placeholder: '请输入联系人手机号',
    rules: [{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码' }],
  },
  {
    label: '预约规则',
    prop: 'reservationRule',
    component: 'input',
    type: 'textarea',
    fullWidth: true,
    placeholder: '请输入会议室预约规则，如：提前1天预约，每次最长使用2小时',
    componentProps: {
      rows: 3,
    },
  },
  {
    label: '会议室描述',
    prop: 'description',
    component: 'input',
    type: 'textarea',
    fullWidth: true,
    placeholder: '请输入会议室详细描述信息',
    componentProps: {
      rows: 3,
    },
  },
  {
    label: '会议室状态',
    prop: 'openStatus',
    component: 'select',
    fullWidth: true,
    required: true,
    placeholder: '请选择会议室状态',
    rules: [{ required: true, message: '请选择会议室状态' }],
    options: Object.values(ROOM_STATUS).map((value) => ({
      label: value,
      value,
    })),
  },
]

export const moduleApi = '/api/conferenceRoomManagement'
export const moduleName = '会议室'

// 格式化表单数据
export const formatFormData = (data: MeetingRoomExtended) => {
  // 复制一份数据，避免直接修改原对象
  const formattedData = { ...data }

  // 处理equipment字段
  if (Array.isArray(formattedData.equipment)) {
    // 处理可能存在的空值，并用逗号拼接
    formattedData.equipment = formattedData.equipment.filter((item) => item && typeof item === 'string' && item.trim()).join(',')
  } else if (formattedData.equipment === undefined || formattedData.equipment === null) {
    // 如果为undefined或null，设置为空字符串
    formattedData.equipment = ''
  }

  return formattedData
}

// 获取设备标签列表
export const getEquipmentTags = (row: MeetingRoomExtended | ApiMeetingRoom) => {
  // 如果不存在设备配置或为空字符串，返回空数组
  if (!row.equipment) return []

  // 如果是字符串类型，按逗号分割并过滤空值
  if (typeof row.equipment === 'string') {
    return row.equipment.split(',').filter((item) => item && item.trim())
  }

  // 如果已经是数组类型，过滤空值并返回
  if (Array.isArray(row.equipment)) {
    return row.equipment.filter((item) => item && typeof item === 'string' && item.trim())
  }

  // 默认返回空数组
  return []
}
